<template>
  <div>
    <el-form label-position="top" label-width="80px" :model="form" ref="form" :rules="rules" :class="{'mobile-form': isMobile}">
      <el-form-item label="试题题干" prop="stem">
        <Editor :stem="form.stem" @changStem="changStem" ref="Editor" />
      </el-form-item>

      <el-form-item label="答案选项" prop="option">
        <el-form-item :label="'[' + list.name + ']'" v-for="list in form.option" :key="list.name" :class="{'mobile-option': isMobile}">
          <el-input v-model="list.content"></el-input>
        </el-form-item>
      </el-form-item>
      <el-form-item label="正确答案" prop="answer">
        <el-radio-group v-model="form.answer" size="small" :class="{'mobile-radio-group': isMobile}">
          <el-radio :label="item.name" border v-for="(item, index) in form.option" :key="index" :class="{'mobile-radio': isMobile}">
            {{ item.name }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="答案解析" prop="explain">
        <el-input type="textarea" autosize v-model="form.explain"></el-input>
      </el-form-item>
      <el-form-item :class="{'mobile-btns': isMobile}">
        <el-button type="primary" :style="{
          backgroundColor: theme,
          borderColor: theme,
          fontWeight: 'bold',
          width: isMobile ? '100%' : 'auto',
          marginBottom: isMobile ? '10px' : '0'
        }" @click="submitForm" v-loading.fullscreen.lock="fullscreenLoading">保存</el-button>
        <el-button @click="$router.push({ name: 'Question' })" :style="{
          width: isMobile ? '100%' : 'auto'
        }">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import Editor from "@/views/vadmin/question/components/editor";

import { mapGetters, mapState } from "vuex";
import variables from "@/assets/styles/variables.scss";
import { addQuestion, updateQuestion } from "@/api/vadmin/question/index";
export default {
  name: "newKnowledge",
  components: { Editor },
  props: ["data"],

  data() {
    return {
      // 表单校验
      rules: {
        stem: [
          { required: true, message: "试题题干不能为空", trigger: "blur" },
        ],
        answer: [
          { required: true, message: "正确答案不能为空", trigger: "blur" },
        ],
        option: [{ required: true, message: "选项不能为空", trigger: "blur" }],
        // explain: [
        //   { required: true, message: "答案解析不能为空", trigger: "blur" },
        // ],
      },
      // 遮罩层
      fullscreenLoading: false,
      form: {
        answer: [],
        option: [
          { name: "A", content: "", is_right: false },
          { name: "B", content: "", is_right: false },
          { name: "C", content: "", is_right: false },
          { name: "D", content: "", is_right: false },
        ],
        explain: null,
        type_name: "单选题",
        type: "1",
        stem: null,
        score: null,
        sort: 1,
      },
      screenWidth: document.body.clientWidth
    };
  },

  computed: {
    ...mapState(["settings"]),
    variables() {
      return variables;
    },
    theme() {
      return this.$store.state.settings.theme;
    },
    isMobile() {
      return this.screenWidth < 768;
    }
  },

  watch: {
    data: {
      handler: function (newValue) {
        this.form = newValue;
      },
      deep: true,
    },
  },

  created() {
    // 监听屏幕宽度变化
    window.addEventListener('resize', this.handleResize);
  },

  mounted() { },
  
  beforeDestroy() {
    // 移除监听器
    window.removeEventListener('resize', this.handleResize);
  },

  methods: {
    handleResize() {
      this.screenWidth = document.body.clientWidth;
    },
    changStem(val) {
      this.form.stem = val;
    },
    reset() {
      this.form = {
        answer: [],
        option: [
          { name: "A", content: "", is_right: false },
          { name: "B", content: "", is_right: false },
          { name: "C", content: "", is_right: false },
          { name: "D", content: "", is_right: false },
        ],
        explain: null,
        type_name: "单选题",
        type: "1",
        stem: null,
        score: null,
        knowledge: "",
        course: "",
        sort: 1,
      };
    },
    /** 提交按钮 */
    submitForm: function () {
      // console.log("题目", this.form);
      this.form.stem = this.$refs["Editor"].getVal();
      this.$refs["form"].validate((valid) => {
        this.form.score = 1; //默认每题1分
        delete this.form["is_right"];
        this.form.option = this.form.option.map((item) => {
          if (item.name == this.form.answer) {
            item.is_right = true;
          } else {
            item.is_right = false;
          }
          return item;
        });

        if (this.form.knowledge == "") {
          this.msgError("知识点选项不可为空");
        } else if (this.form.course == "") {
          this.msgError("请选择课程");
        } else {
          if (valid) {
            this.fullscreenLoading = true;
            if (this.form.id != undefined) {
              updateQuestion(this.form).then((response) => {
                this.fullscreenLoading = false;
                this.msgSuccess("修改成功");
              });
            } else {
              addQuestion(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.fullscreenLoading = false;
                this.reset();
              });
            }
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.mobile-form {
  .el-form-item {
    margin-bottom: 20px;
  }
  
  .el-form-item__label {
    padding: 0 0 8px;
    line-height: 1.2;
    text-align: left;
  }
}

.mobile-option {
  margin-bottom: 15px;
  padding: 10px;
  border-radius: 6px;
  background-color: #f9f9f9;
  
  .el-form-item__label {
    font-weight: bold;
    padding-bottom: 5px;
  }
  
  .el-input {
    width: 100%;
  }
}

.mobile-radio-group {
  display: flex;
  flex-direction: column;
  width: 100%;
  
  .el-radio {
    margin-right: 0;
    margin-bottom: 10px;
    padding: 8px 15px;
    width: 100%;
    border-radius: 6px;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    .el-radio__label {
      font-size: 14px;
    }
  }
}

.mobile-btns {
  margin-top: 30px;
  
  .el-button {
    margin-left: 0;
  }
}
</style>

